/*
    Copyright 2010 Northbranchlogic, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.runjrun;

import com.norbl.util.*;

/**
 *
 * @author Barnet Wagman
 */

public class TaskState extends DbEl {

    public enum Status {
        waiting, running, finished, terminatedByException,
        missingRunnable, orphan, unrunnable
    }

    public String taskName;
    public TaskDescription taskDes;
    public Status status;
    public String host;
    public String instanceID;
    public Long statusTime;
    public Long submissionTime;
    public Long startTime;
    public Long finishedTime;

    public TaskState(String taskName,
                    TaskDescription taskDes,
                    Status status,
                    String host,
                    String instanceID,
                    Long statusTime,
                    Long submissionTime,
                    Long startTime,
                    Long finishedTime) {
        this.taskName = taskName;
        this.taskDes = taskDes;
        this.status = status;
        this.host = host;
        this.instanceID = instanceID;
        this.statusTime = statusTime;
        this.submissionTime = submissionTime;
        this.startTime = startTime;
        this.finishedTime = finishedTime;
    }

    public TaskState() {}

    public TaskState(PropertyList pl,boolean allowMissingFields) {
        super(pl,allowMissingFields);
    }

    public DbElField[] getRelevantFields() {

        try {
            Class c = TaskState.class;
            return( new DbElField[] {
                new DbElField(c.getField("taskName"),taskName,"Name"),
                new DbElField(c.getField("taskDes"),taskDes,"TaskDes"),
                new DbElField(c.getField("host"),host,"Host"),
                new DbElField(c.getField("instanceID"),instanceID,"Instance ID"),
                new DbElField(c.getField("status"),status,"Status"),
                new TimeField(c.getField("statusTime"),statusTime,"Time"),
                new TimeField(c.getField("submissionTime"),
                            submissionTime,"Submitted"),
                new TimeField(c.getField("startTime"),startTime,"Started"),
                new TimeField(c.getField("finishedTime"),finishedTime,
                            "Finished")
            });
        }
        catch(Exception x) { throw new RuntimeException(x); }
    }

     public DbElField[] getDisplayFields() {

        try {
            Class c = TaskState.class;
            return( new DbElField[] {
                new DbElField(c.getField("taskName"),taskName,"Name"),
                new DbElField(c.getField("host"),host,"Host"),
                new DbElField(c.getField("instanceID"),instanceID,"Instance ID"),
                new DbElField(c.getField("status"),status,"Status"),
                new TimeField(c.getField("statusTime"),statusTime,"Time"),
                new TimeField(c.getField("submissionTime"),
                            submissionTime,"Submitted"),
                new TimeField(c.getField("startTime"),startTime,"Started"),
                new TimeField(c.getField("finishedTime"),finishedTime,
                            "Finished")
            });
        }
        catch(Exception x) { throw new RuntimeException(x); }
    }

    public Object getPropertyValue(Property p) {

        if ( p == null ) return(null);
        if ( p.value == null ) return(null);
        if ( p.value instanceof TaskDescription )
            return( ((TaskDescription) p.value).toMessageString() );
        if ( p.name.equals("taskDes") && (p.value instanceof String) )
            return(new TaskDescription((String) p.value));
        if ( p.name.equals("status") && (p.value instanceof String) )
            return( TaskState.Status.valueOf( (String) p.value ) );
        else return(p.value);
    }

    public String toString() {
        return(taskName + " " +
               taskDes + " " +
               status + " " +
               host + " " +
               instanceID + " " +
               statusTime);

    }
}
